package com.example.payment.strategy.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alipay.api.AlipayApiException;
import com.example.model.check_in.CheckIn;
import com.example.model.payment.Payment;
import com.example.model.payment.PaymentResultVo;
import com.example.payment.service.AliPayService;
import com.example.payment.strategy.PayTypeStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component
public class AlipayStrategyImpl implements PayTypeStrategy {

    @Autowired
    private AliPayService aliPayService;

    @Override
    public PaymentResultVo pay(Payment payment, BigDecimal amount, BigDecimal pay) throws AlipayApiException {
        String response = aliPayService.createPreOrder(payment.getPaymentId().toString(), amount.toString(), "MISP酒店支付测试支付测试").getBody();
        String replace = response.replace("\\", "");
        JSONObject responseJson = JSONUtil.parseObj(replace);
        JSONObject entries = responseJson.getJSONObject("alipay_trade_precreate_response");
        PaymentResultVo paymentResultVo = new PaymentResultVo();
        paymentResultVo.setQrCode(entries.get("qr_code").toString());
        paymentResultVo.setPaymentId(payment.getPaymentId());
        return paymentResultVo;
    }
    @Override
    public void refund(Long paymentId, BigDecimal amount) throws AlipayApiException {
       aliPayService.AlipayTradeRefund(paymentId, amount.toString());

    }
}
